﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.Drawing;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// TMS_获取SRM价格库_路线价格
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SRMtoTMSPriceCreateController : ControllerBase
    {
        private readonly ILogger<SRMtoTMSPriceCreatRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string srm_con;


        public SRMtoTMSPriceCreateController(ILogger<SRMtoTMSPriceCreatRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");
            srm_con = _configuration.GetConnectionString("SRM_DbConnection");

        }

        /// <summary>
        /// TMS_获取SRM价格库_路线价格
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostTMS_PrcieGet([FromBody] SRMtoTMSPriceCreatRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMS_BillCreateResponseDto re = new TMS_BillCreateResponseDto();
            string source_no = strparm.source_no;
            string price_no = "SRM"+"-"+DateTime.Now.ToString("yyMMdd");
            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic


            try
            {
                //同步TMS现有运价状态
                dic = new Dictionary<string, string>(); //查询公用dic
                dic.Add("source_no", source_no);//查询对应物流计划
                DataTable tms_yf_dt = MethodHelp.GetTableInfo("TMS0109", dic, con);//获取TMS生效明细
                if (tms_yf_dt.Rows.Count > 0)
                {
                    foreach (DataRow row in tms_yf_dt.Rows)
                    {
                        dic = new Dictionary<string, string>(); //查询公用dic
                        dic.Add("source_from_num", row["source_no"].ToString());
                        dic.Add("item_code", row["item_code"].ToString());
                        dic.Add("supplier_company_num", row["carrierid_code"].ToString());
                        DataTable srm_dt = MethodHelp.GetOutsideTableInfo("TMS0110", dic, srm_con, con);
                        if (srm_dt.Rows.Count <= 0)
                        {
                            dic = new Dictionary<string, string>(); //查询公用dic
                            dic.Add("source_no", row["source_no"].ToString());
                            dic.Add("item_code", row["item_code"].ToString());
                            dic.Add("carrierid_code", row["carrierid_code"].ToString());
                            string strSQL = MethodHelp.GetTranSQL("TMS0111", dic, con);
                            if (strSQL != "")
                            {
                                strSqlList.Add(strSQL.ToString());
                            }
                            else
                            {
                                strMsg = "Parameter conversion failed";
                            }
                        }

                    }
                }


                //新增SRM新增路线运价
                dic = new Dictionary<string, string>(); //查询公用dic
                dic.Add("source_no", source_no);//查询对应物流计划
                DataTable fy_dt = MethodHelp.GetOutsideTableInfo("TMS0101", dic,srm_con, con);//获取物流计划信息
                if (fy_dt.Rows.Count>0)
                {
                    foreach (DataRow item in fy_dt.Rows)
                    {
                        string factory_code = "";
                        string cartype_code = "";
                        string cartype_name = "";
                        string price_type = "";
                        dic = new Dictionary<string, string>(); //查询公用dic
                        dic.Add("source_no", item["source_from_num"].ToString());
                        dic.Add("item_code", item["smdm_item_code"].ToString());
                        dic.Add("carrierid_code", item["supplier_company_num"].ToString());
                        DataTable check_dt = MethodHelp.GetTableInfo("TMS0107", dic, con);//获取是否存价格记录
                        if (check_dt.Rows.Count<=0)
                        {

                            //获取TMS基地编码
                            if (item["company_name"].ToString().Contains("金湾"))
                            {
                                factory_code = "[\"tms_zh\"]";
                            }else if (item["company_name"].ToString().Contains("四川"))
                            {
                                factory_code = "[\"tms_sc\"]";
                            }else if (item["company_name"].ToString().Contains("广州"))
                            {
                                factory_code = "[\"tms_gz\"]";
                            }else if (item["company_name"].ToString().Contains("青海"))
                            {
                                factory_code = "[\"tms_qh\"]";
                            }
                            else
                            {
                                factory_code = "[\"tms_gd\"]";
                            }
                            //获取价格类型
                            if (item["smdm_item_code"].ToString().Contains("R"))
                            {
                                price_type = "往返同价";
                            }
                            if (item["smdm_item_code"].ToString().Contains("U"))
                            {
                                price_type = "单向计价";
                            }
                            if (item["smdm_item_code"].ToString().Contains("T"))
                            {
                                price_type = "往返总价";
                            }





                            //获取TMS车型编码及车型名称
                            dic = new Dictionary<string, string>(); //查询公用dic
                            dic.Add("compare_code", item["compare_code"].ToString());
                            DataTable dt_cartype= MethodHelp.GetTableInfo("TMS0108", dic, con);
                            if (dt_cartype.Rows.Count>0)
                            {
                                cartype_code = dt_cartype.Rows[0]["cartypecode"].ToString();
                                cartype_name = dt_cartype.Rows[0]["cartypename"].ToString();
                            }

                            dic = new Dictionary<string, string>(); //查询公用dic
                            dic.Add("source_no", item["source_from_num"].ToString());
                            dic.Add("factory_code", factory_code);
                            dic.Add("carrierid_code", item["supplier_company_num"].ToString());
                            dic.Add("carrierid_name", item["supplier_company_name"].ToString());
                            dic.Add("item_code", item["smdm_item_code"].ToString());
                            dic.Add("item_name", item["smdm_item_name"].ToString());
                            dic.Add("cartype_code", cartype_code);
                            dic.Add("cartype_name", cartype_name);
                            dic.Add("effective_date", item["valid_date_from"].ToString());
                            dic.Add("expiring_date", item["valid_date_to"].ToString());
                            dic.Add("start_province", item["attribute_varchar10"].ToString());
                            dic.Add("start_city", item["attribute_varchar11"].ToString());
                            dic.Add("end_province", item["attribute_varchar13"].ToString());
                            dic.Add("end_city", item["attribute_varchar14"].ToString());
                            dic.Add("compute_ploy", item["attribute_varchar16"].ToString());
                            dic.Add("compute_mode", item["attribute_varchar17"].ToString());
                            dic.Add("begin_range", item["attribute_varchar19"].ToString());
                            dic.Add("stop_range", item["attribute_varchar20"].ToString());
                            dic.Add("tax_rate", item["tax_rate"].ToString());
                            dic.Add("tax_price", item["tax_included_price"].ToString());
                            dic.Add("untax_price", item["net_price"].ToString());
                            dic.Add("is_enable", "1");
                            dic.Add("factory_name", item["company_name"].ToString());
                            dic.Add("source_sys", "SRM");
                            dic.Add("price_type", price_type);
                            dic.Add("price_no", price_no+ item["price_lib_id"].ToString());
                            string strSQL = MethodHelp.GetTranSQL("TMS0100", dic,con);
                            if (strSQL != "")
                            {
                                strSqlList.Add(strSQL.ToString());
                            }
                            else
                            {
                                strMsg = "Parameter conversion failed";
                            }
                        }

                    }                   
                }

                if (strMsg == "")
                {
                    if (strSqlList.Count > 0)
                    {
                        bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                        if (A)
                        {

                        }
                        else
                        {
                            strMsg = "Database update failed";
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
                    
            if (strMsg=="")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
